/*********************************************************************************
 *      Copyright:  (C) 2025 LingYun<iot25@lingyun>
 *                  All rights reserved.
 *
 *       Filename:  pack_data.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(09/04/25)
 *         Author:  LingYun <iot25@lingyun>
 *      ChangeLog:  1, Release initial version on "09/04/25 15:22:31"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <string.h>


#include "logger.h"
#include "pack_data.h"

int pack_data(packet_t *pack, char *data, int bytes)
{
	if ( NULL == pack )
	{
		log_error("pointer [pack] is NULL");
		return -1;
	}
	memset(data, 0, bytes);
	snprintf(data, bytes, "id:%s|time:%s|temp:%f", pack->dev_id, pack->dev_time, pack->dev_temp);
	log_debug("pack_data succesful");
	return 0;
}

int unpack_data(char *data, packet_t *pack)
{
	if ( NULL == data )
	{
		log_error("pointer [data] if NULL");
		return -1;
	}
	memset(pack, 0, sizeof(packet_t));
	sscanf(data, "id:%[^|]|time:%[^|]|temp:%f", pack->dev_id, pack->dev_time, &pack->dev_temp);
	log_debug("unpack_data successful");
	return 0;
}

